﻿//Copyright (c) Chris Pietschmann 2013 (http://pietschsoft.com)
//Licensed under the GNU Library General Public License (LGPL)
//License can be found here: http://easypatterns.codeplex.com/license

using EasyPatterns.Bridge;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace EasyPatterns.Test.Bridge
{
    [TestClass]
    public class BridgeTest
    {
        [TestMethod]
        public void Constructor_001()
        {
            var person = new Person { Name = "Steve", Age = 12 };
            var target = new MyPersonBridge(person);

            Assert.IsNotNull(target);
            Assert.IsInstanceOfType(target, typeof(Bridge<Person>));
        }

        [TestMethod]
        public void Constructor_002()
        {
            var person = new Person { Name = "Steve", Age = 12 };
            var target = new MyPersonBridge(person);

            Assert.IsNotNull(target);
            Assert.IsInstanceOfType(target, typeof(IBridge<Person>));
        }

        [TestMethod]
        public void GetNameOperation_001()
        {
            var person = new Person { Name = "John", Age = 56 };
            var target = new MyPersonBridge(person);

            var actual = target.GetNameOperation();
            Assert.AreEqual("John", actual);
        }

        [TestMethod]
        public void Proxy_Using_Bridge_Name_001()
        {
            var person = new Person { Name = "Chris", Age = 100 };
            var target = new PersonProxy(person);

            var actual = target.Name;

            Assert.AreEqual("Chris", actual);
        }

        [TestMethod]
        public void Proxy_Using_Bridge_Name_002()
        {
            var person = new Person { Name = "Chris", Age = 100 };
            var target = new PersonProxy(person);

            target.Name = "John";

            var actual = target.Name;

            Assert.AreEqual("John", actual);
        }
        [TestMethod]
        public void Proxy_Using_Bridge_Age_001()
        {
            var person = new Person { Name = "Chris", Age = 100 };
            var target = new PersonProxy(person);

            var actual = target.Age;

            Assert.AreEqual("100", actual);
        }

        [TestMethod]
        public void Proxy_Using_Bridge_Age_002()
        {
            var person = new Person { Name = "Chris", Age = 100 };
            var target = new PersonProxy(person);

            target.Age = 5;

            var actual = target.Age;

            Assert.AreEqual(5, actual);
        }

        public class PersonProxy : Bridge<Person>, IPerson
        {
            public PersonProxy(Person implementation)
                : base(implementation)
            { }

            public string Name
            {
                get
                {
                    return this.Implementation.Name;
                }
                set
                {
                    this.Implementation.Name = value;
                }
            }

            public int Age
            {
                get
                {
                    return this.Implementation.Age;
                }
                set
                {
                    this.Implementation.Age = value;
                }
            }
        }

        public class MyPersonBridge : Bridge<Person>
        {
            public MyPersonBridge(Person implementation)
                : base(implementation)
            { }

            public string GetNameOperation()
            {
                return this.Implementation.Name;
            }
        }
    }
}
